const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');

app.get('/getallretgoods',(req,res) => {
	var sql = $sql.retgoods_sql.getallretgoods;
	connection.query(sql,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据账号获取退货记录
app.post('/getretbyacc',(req,res) => {
	var sql = $sql.retgoods_sql.getretbyacc;
	var params = [req.body.account]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据完成状态获取退货记录
app.post('/getretbywea',(req,res) => {
	var sql = $sql.retgoods_sql.getretbywea;
	var params = [req.body.whether,req.body.account]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id获取退货记录
app.post('/getretbyid',(req,res) => {
	var sql = $sql.retgoods_sql.getretbyid;
	var params = [req.body.id]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id删除退货记录
app.delete('/delretbyid',(req,res) => {
	var sql = $sql.retgoods_sql.delretbyid;
	var params = [req.body.id]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新退货请求
app.put('/upret', (req, res) => {
	var sql = $sql.retgoods_sql.upret;
    var params = [req.body.wname,req.body.data,req.body.stoname,req.body.stoid,req.body.num,req.body.stospe,
				 req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,
				 req.body.whentryname,req.body.title,req.body.id]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新退货请求
app.put('/addret', (req, res) => {
	var sql = $sql.retgoods_sql.upret;
    var params = [req.body.wname,req.body.data,req.body.stoname,req.body.stoid,req.body.num,req.body.stospe,
				 req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,
				 req.body.whentryname,req.body.title,req.body.wname]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

module.exports = app;